<template>
  <d2-container>
    <div class="flex-1 mx-3">
      <div class="font-s-3 ">搜索设置 --- 关键词</div>
      <div class="mt-3">
        <el-button type="warning" size="medium" @click="addKeyword" icon="el-icon-plus">添加关键词</el-button>
      </div>
      <div class="mt-3" style="width: 60%;">
        <el-table :data="keywordList" stripe size="medium">
          <el-table-column align="center" label="序号" type="index" width="50" />
          <el-table-column label="关键词" prop="keyword" width="90" />
          <el-table-column label="排序" prop="sort" width="90" />
          <el-table-column align="center" label="操作" width="130">
            <template slot-scope="scope">
              <el-button size="mini" @click="handleEdit(scope.row.id)">修改</el-button>
              <el-button size="mini" type="danger" @click="openDelete(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <el-dialog :visible.sync="dialogVisible" width="30%" title="产地">
      <el-form :model="formKeyword" ref="form" label-width="80px">
        <el-form-item label="路由名称" prop="keyword">
          <div style="width: 60%;">
            <el-input v-model="formKeyword.keyword"></el-input>
          </div>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <div style="width: 40%;">
            <el-input v-model="formKeyword.sort"></el-input>
          </div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </d2-container>
</template>

<script>
  import {
    keywordListUrl,
    keywordCreateUrl,
    keywordDetailUrl,
    keywordUpdateUrl,
    keywordDeleteUrl,
  } from "@/utils/clientistration"
  export default {
    data() {
      return {
        dialogVisible: false,
        addStatus: true,
        queryInfo: {
          page: 1
        },
        keywordList: [],
        keywordTotal: 0,
        formKeyword: {
          keyword: '',
          sort: ''
        },
        getKeywordId: ''
      }
    },
    created() {
      this.initData()
    },
    methods: {
      initData() {
        this.getKeywordListList()
      },
      getKeywordListList() {
        this.$get(keywordListUrl, this.queryInfo).then(res => {
          this.keywordList = res.data.data.list
          this.keywordTotal = res.data.data.total
          console.log(this.keywordList);
        })
      },
      addKeyword() {
        this.dialogVisible = true
        this.addStatus = true
      },
      handleEdit(kywordId) {
        this.dialogVisible = true
        this.addStatus = false
        this.getKeywordId = kywordId
        this.$get(keywordDetailUrl + kywordId + '/').then(res => {
          console.log(res.data.code)
          if (res.data.code == 200) {
            this.formKeyword = {
              keyword: res.data.data.keyword,
              sort: res.data.data.sort
            }
            console.log(this.formKeyword.keyword);
          }
        })
      },
      submitForm() {
        if (this.addStatus) {
          this.$post(keywordCreateUrl, this.formKeyword).then(res => {
            if (res.data.code == 200) {
              this.dialogVisible = false
              this.$message.success('添加成功')
              this.formKeyword = {}
              this.getKeywordListList()
            }
          })
        } else {
          this.$put(keywordUpdateUrl + this.getKeywordId + '/', this.formKeyword).then(res => {
            if (res.data.code == 200) {
              this.dialogVisible = false
              this.$message.success('修改成功')
              this.formKeyword = {}
              this.getKeywordListList()
            }
          })
        }
      },
      openDelete(keywordId) {
        this.$confirm('此操作将永久删除该路由, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          // 在确定时执行handleDelete的删除
          this.$del(keywordDeleteUrl + keywordId + '/').then(res => {
            if (res.data.code == 200) {
              this.$message({
                type: 'success',
                message: '删除成功!'
              });
              this.getKeywordListList()
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      }
    }
  }
</script>

<style>
</style>
